<?php

namespace App\Service\Message\v1_0_4;

use App\Model\User;
use App\Model\UserProfile;
use App\Model\UserRecOnline;
use App\Service\BaseService;

/**
 * 消息中心 - 用户上线批量推荐
 */
class OnlineRecUserService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $profile = UserProfile::getProfile($userId);
        $option = $this->request->input('option', 'data');

        //男用户，不给推荐
        if ($profile->gender == 1) return ['list' => [], 'new' => 0];

        //获取新推荐个数
        if ($option == 'new') {
            return [
                'list' => [],
                'new' => UserRecOnline::getNew($userId)
            ];
        }

        $ret['list'] = [];
        $ret['user_id'] = $userId;
        $userList = UserRecOnline::getList($userId);
        foreach ($userList as $userItem) {
            //该批用户ID
            $userIds = $userItem['user_ids'];
            $users = UserProfile::getUsersProfile($userIds);
            $imAccounts = User::getImAccounts($userIds);
            $listItem = ['users' => [], 'rec_time' => $userItem['rec_time']];
            array_walk($users, function ($user) use (&$listItem, $imAccounts) {
                $item = UserProfile::buildRecItem($user, $imAccounts);
                array_push($listItem['users'], $item);
            });
            array_push($ret['list'], $listItem);
        }
        return $ret;
    }
}
